package com.zc.web.controller.timedown;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.zc.web.domain.ImportantLifeEvent;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zc.common.annotation.Log;
import com.zc.common.core.controller.BaseController;
import com.zc.common.core.domain.AjaxResult;
import com.zc.common.enums.BusinessType;
import com.zc.web.service.IImportantLifeEventService;
import com.zc.common.utils.poi.ExcelUtil;
import com.zc.common.core.page.TableDataInfo;

/**
 * 重要人生节点事件Controller
 * 
 * @author zc
 * @date 2025-10-03
 */
@RestController
@RequestMapping("/wx/event")
public class ImportantLifeEventController extends BaseController
{
    @Autowired
    private IImportantLifeEventService importantLifeEventService;

    /**
     * 查询重要人生节点事件列表
     */
    @PreAuthorize("@ss.hasPermi('wx:event:list')")
    @GetMapping("/list")
    public TableDataInfo list(ImportantLifeEvent importantLifeEvent)
    {
        startPage();
        List<ImportantLifeEvent> list = importantLifeEventService.selectImportantLifeEventList(importantLifeEvent);

        // 获取分页信息
        TableDataInfo pageInfo = getDataTable(list);

        // 对数据进行处理
        List<ImportantLifeEvent> processedList = ((List<ImportantLifeEvent>) pageInfo.getRows()).stream().map(event -> {
            if (event.getEventDate() != null) {
                LocalDate eventLocalDate = event.getEventDate();
                long daysUntil = ChronoUnit.DAYS.between(LocalDate.now(), eventLocalDate);
                event.setDaysUntil(Math.toIntExact(daysUntil));
            }
            return event;
        }).collect(Collectors.toList());

        // 重新设置处理后的数据
        pageInfo.setRows(processedList);
        return pageInfo;
    }

    /**
     * 导出重要人生节点事件列表
     */
    @PreAuthorize("@ss.hasPermi('wx:event:export')")
    @Log(title = "重要人生节点事件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImportantLifeEvent importantLifeEvent)
    {
        List<ImportantLifeEvent> list = importantLifeEventService.selectImportantLifeEventList(importantLifeEvent);
        ExcelUtil<ImportantLifeEvent> util = new ExcelUtil<ImportantLifeEvent>(ImportantLifeEvent.class);
        util.exportExcel(response, list, "重要人生节点事件数据");
    }

    /**
     * 获取重要人生节点事件详细信息
     */
    @PreAuthorize("@ss.hasPermi('wx:event:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(importantLifeEventService.selectImportantLifeEventById(id));
    }

    /**
     * 新增重要人生节点事件
     */
    @PreAuthorize("@ss.hasPermi('wx:event:add')")
    @Log(title = "重要人生节点事件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImportantLifeEvent importantLifeEvent)
    {
        return toAjax(importantLifeEventService.insertImportantLifeEvent(importantLifeEvent));
    }

    /**
     * 修改重要人生节点事件
     */
    @PreAuthorize("@ss.hasPermi('wx:event:edit')")
    @Log(title = "重要人生节点事件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImportantLifeEvent importantLifeEvent)
    {
        return toAjax(importantLifeEventService.updateImportantLifeEvent(importantLifeEvent));
    }

    /**
     * 删除重要人生节点事件
     */
    @PreAuthorize("@ss.hasPermi('wx:event:remove')")
    @Log(title = "重要人生节点事件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(importantLifeEventService.deleteImportantLifeEventByIds(ids));
    }
}
